<template>
  <div class="pd30 produceFactory">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>地产品牌管理</div>
        <!-- <el-button class="addBtn fw defaultbtn"  type="primary">添加</el-button> -->
      </div>
      <div class="con">
        <div class="con-row flex flexac pl search-box">
          <!-- <div class="flex flexac">
            <el-input v-model="param.id" class="co-input" placeholder="请输入电梯ID..." />
          </div> -->
          <div class="flex flexac">
            <el-input v-model="param.name" class="co-input" placeholder="请输入电梯品牌名称..." @input="getList" />
          </div>
          <div class="groupbtn flex m-l-40 ">
            <el-button class="flex flexac m-r-20 fs14" size="small" type="warning" @click="getList"><i class="co-i" /><span class="search1">查 询</span></el-button>
            <el-button class="co-empty flexcenter m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清 空</el-button>
          </div>
        </div>
        <div class="con-row flex flexac flexjcsb pl">
          <el-button class="defaultbtn" type="primary" @click="handleRefresh"><i class="el-icon-refresh" :class="refresh?'goRefresh':''" />刷 新</el-button>
          <div class="m-r-25 flex">
            <el-button class="defaultbtn" type="primary" @click="handleAdd"><i class="el-icon-circle-plus-outline" />添加</el-button>
            <el-button class="defaultbtn" type="primary" @click="handleExportExcel"><i class="iconfont icon-wenjiandaochu" />导出</el-button>
            <div class="pre m-l-10">
              <el-button class="defaultbtn pre" type="primary" @click="handleSenior('field')"><i class="iconfont icon-shezhi" />字段
              </el-button>
              <div class="selectUl" :class="seniorSet.field?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" :key="i" class="li">
                  <el-checkbox v-model="item.isShow">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <div class="pre m-l-10">
              <el-button class="defaultbtn" type="primary" @click="handleSenior('sortable')"><i class="iconfont icon-shezhi" />排序</el-button>
              <div class="selectUl" :class="seniorSet.sortable?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" v-if="item.isShow" :key="i" class="li">
                  <el-checkbox v-model="item.sortable">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <!-- <el-button class="defaultbtn" @click="handleExportExcel" type="primary"><i class="iconfont icon-shezhi"></i>固定</el-button> -->
          </div>
        </div>

        <el-table
          id="factorytable"
          ref="factorytable"
          class="con-table"
          type="expand"
          :data="factorytable"
          style="width: 100%"
          highlight-current-row
        >
          <template v-for="item in tablehead">
            <el-table-column v-if="item.isShow" :key="item.index" :width="item.width" align="center" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
          </template>
          <el-table-column prop="name" align="center" label="基本操作">
            <template slot-scope="scope">
              <el-button class="defaultbtn" type="primary" plain size="mini" @click="handleEdit(scope.$index, scope.row)">
                <i class="el-icon-edit" />
                编辑</el-button>
              <el-button type="danger" class="defaultbtn" @click="handleDeleteBtn(scope.$index, scope.row)"><i class="el-icon-delete-solid" />删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination class="paginationWrap flex " :current-page="paging.currentPage" :page-sizes="paging.pageSizeArr" :page-size="paging.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="paging.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
    <el-dialog class="bgtransparent" :visible.sync="elevatorDialog">
      <elevatorDialog :data="editFactory" :is-edit="isEdit" @handleRefresh="handleRefresh" @handleCloseDialog="handleCloseDialog" />
    </el-dialog>
  </div>
</template>

<script>
import elevatorDialog from './dialog'
import { fetchList, fetchDelete } from '@/api/baseInfo/landbrand'
import { showNotice, formatDateTime } from '@/utils/toolclass'

export default {
  name: 'Unnamed',
  components: {
    elevatorDialog
  },
  props: {},
  data() {
    return {
      paging: {
        currentPage: 1,
        pageSize: 10,
        pageSizeArr: [10, 20, 50, 100],
        total: 0
      },

      factorytable: [],
      param: {
        id: '',
        name: ''
      },
      elevatorDialog: false,
      editFactory: {},
      isEdit: false,
      selectDataLength: 0,
      refresh: false,
      seniorSet: {
        'field': false,
        'sortable': false
      },
      tablehead: [
        {
          index: 0,
          prop: 'id',
          label: '地产品牌ID',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'name',
          label: '地产品牌名称',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'nameSimple',
          label: '地产品牌简称',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 3,
          prop: 'remark',
          label: '备注',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ]

    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {
    this.$storage.set('upUrl', JSON.stringify({ 'path': '/basicsdata/relation/landBrand/index' }))
  },
  methods: {
    handleSenior(type) {
      if (type === 'field') {
        this.seniorSet.field = !this.seniorSet.field
      }
      if (type === 'sortable') {
        this.seniorSet.sortable = !this.seniorSet.sortable
      }
    },

    handleExportExcel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('@/excel/Export2Excel')
        const tHeader = ['地产ID', '地产品牌名称', '地产品牌名称', '备注']
        const filterVal = ['id', 'name','nameSimple', 'remark']
        const data = this.formatJson(filterVal, this.factorytable)
        export_json_to_excel(tHeader, data, '地产品牌列表(' + formatDateTime().ymd) + ')'
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleRefresh() {
      this.refresh = true
      this.getList()
      setTimeout(() => {
        this.refresh = false
      }, 1200)
    },
    getList() {
      fetchList(this.param.id, this.param.name, this.paging.currentPage, this.paging.pageSize).then((res) => {
        if (res.code === '000000') {
          // console.log(res)
          this.factorytable = res.data;
          this.paging = {
            currentPage: res.pager.pageIndex,
            pageSize: res.pager.pageSize,
            total: res.pager.totalSize
          }
        }
      })
    },
    handleEmptySearch() {
      this.param.id = ''
      this.param.name = ''
      this.getList()
    },
    handleEdit(index, row) {
      console.log(row)
      this.elevatorDialog = true
      this.isEdit = true
      this.editFactory = row
    },
    handleAdd() {
      this.elevatorDialog = true
      this.isEdit = false
      this.editFactory = {
        id: null,
        name: '',
        nameSimple:'',
        remark: '',
        version: null
      }
    },
    handleDeleteBtn(index, row) {
      this.$alert('这个操作会删除和它相关联的所有数据,确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        callback: action => {
          if (action === 'confirm') {
            this.handleDelete(row.id)
          }
        }
      })
    },
    handleDelete(id) {
      fetchDelete(id).then((res) => {
        if (res.code === '000000') {
          showNotice('success', '提示', '删除成功')
          this.getList()
        }
      })
    },
    handleCloseDialog() {
      this.elevatorDialog = !this.elevatorDialog
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.paging.currentPage = val
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
